package cn.wolfcode.wolf2w.service.imp;

import cn.wolfcode.wolf2w.cache.service.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.DBHelper;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.RedisKeys;
import cn.wolfcode.wolf2w.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.repository.support.QuerydslAbstractMongodbQuery;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
//@Transactional mongodb事务必须是复制集方式
public class StrategyServiceImpl implements IStrategyService {


    @Autowired
    private StrategyRepository repository;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private StrategyCatalogServiceImpl strategyCatalogService;


    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IDestinationService destinationService;

    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        repository.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        repository.save(strategy);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        return repository.findAll();
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        //查询对象
        Query query = new Query();

        Long total = template.count(query, Strategy.class);
        if (total == 0) {
            return Page.empty();//空的分页对象
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "_id");
        return DBHelper.query(template, Strategy.class, query, pageable);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        StrategyCatalog catalog = strategyCatalogService.get(strategy.getCatalogId());


        strategy.setDestId(catalog.getDestId());
        strategy.setDestName(catalog.getDestName());
        StrategyTheme strategyTheme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(strategyTheme.getName());
        strategy.setCatalogName(strategy.getCatalogName());


        //面包屑

        List<Destination> totas = destinationService.queryToasts(catalog.getDestId());
        if (totas != null && totas.size() > 0) {
            Destination destination = totas.get(0);
            if ("中国".equals(destination)) {
                strategy.setIsabroad(Strategy.ABROAD_NO);
            } else {
                strategy.setIsabroad(Strategy.ABROAD_YES);
            }
        }
        if (StringUtils.hasLength(strategy.getId())) {
            //更新
            //1.查询
            Strategy temp = this.get(strategy.getId());
            //2.替换
            strategy.setCreateTime(temp.getCreateTime());
            strategy.setViewnum(temp.getViewnum());
            strategy.setReplynum(temp.getReplynum());
            strategy.setThumbsupnum(temp.getThumbsupnum());
            strategy.setFavornum(temp.getFavornum());
            strategy.setSharenum(temp.getSharenum());
            //3.更新
            this.update(strategy);
        } else {
            strategy.setCreateTime(new Date());
            this.save(strategy);
        }


    }


    @Override
    public void changeState(String id, int state) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));

        Update update = new Update();
        update.set("state", state);
        template.updateMulti(query, update, Strategy.class);
    }

    @Override
    public List<Strategy> queryCatalogId(String id) {
        return repository.findByCatalogId(id);
    }

    @Override
    public List<Strategy> queryViewnumTop3(String destId) {


        Pageable pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");

        List<Strategy> list = repository.findByDestId(destId, pageable);
        return list;
    }

    @Override
    public void updateStrategtVo(StrategyStatisVO strategyStatisVO) {
        Query query = new Query();

        query.addCriteria(Criteria.where("_id").is(strategyStatisVO.getStrategyId()));

        Update update = new Update();


        update.set("viewnum", strategyStatisVO.getViewnum());
        update.set("replunum", strategyStatisVO.getReplynum());
        update.set("favornum", strategyStatisVO.getFavornum());
        update.set("sharenum", strategyStatisVO.getSharenum());
        update.set("thumbsupnum", strategyStatisVO.getThumbsupnum());


        template.updateMulti(query, update, Strategy.class);
    }


}
